home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Languages / MPW Oberon 2.1168 / OExamples / TESample.mod < prev    next >
Encoding:
Text File  |  1995-07-03  |  55.9 KB  |  1,652 lines  |  [TEXT/MPS ]

  1. (*------------------------------------------------------------------------------
  2. #
  3. #    Apple Macintosh Developer Technical Support
  4. #
  5. #    MultiFinder-Aware Simple TextEdit Sample Application
  6. #
  7. #    TESample
  8. #
  9. #    TESample.p    -    Pascal Source
  10. #
  11. #    Copyright © 1989 Apple Computer, Inc.
  12. #    All rights reserved.
  13. #
  14. #    Versions:    
  15. #                1.00                08/88
  16. #                1.01                11/88
  17. #                1.02                04/89
  18. #                1.03                06/89
  19. #
  20. #    Components:
  21. #                TESample.p            Feb.  1, 1990
  22. #                TESampleGlue.a        Feb.  1, 1990
  23. #                TESample.r            Feb.  1, 1990
  24. #                TESample.h            Feb.  1, 1990
  25. #                TESample.make        Feb.  1, 1990
  26. #
  27. #    TESample is an example application that demonstrates how 
  28. #    to initialize the commonly used toolbox managers, operate 
  29. #    successfully under MultiFinder, handle desk accessories and 
  30. #    create, grow, and zoom windows. The fundamental TextEdit 
  31. #    toolbox calls and TextEdit autoscroll are demonstrated. It 
  32. #    also shows how to create and maintain scrollbar controls.
  33. #
  34. #    It does not by any means demonstrate all the techniques you 
  35. #    need for a large application. In particular, Sample does not 
  36. #    cover exception handling, multiple windows/documents, 
  37. #    sophisticated memory management, printing, or undo. All of 
  38. #    these are vital parts of a normal full-sized application.
  39. #
  40. #    This application is an example of the form of a Macintosh 
  41. #    application; it is NOT a template. It is NOT intended to be 
  42. #    used as a foundation for the next world-class, best-selling, 
  43. #    600K application. A stick figure drawing of the human body may 
  44. #    be a good example of the form for a painting, but that does not 
  45. #    mean it should be used as the basis for the next Mona Lisa.
  46. #
  47. #    We recommend that you review this program or Sample before 
  48. #    beginning a new application. Sample is a simple app. which doesn’t 
  49. #    use TextEdit or the Control Manager.
  50. #
  51. ------------------------------------------------------------------------------*)
  52.  
  53.  
  54. MODULE TESample;
  55.  
  56.  
  57. (*Segmentation strategy:
  58.  
  59.  This program consists of three segments. Main contains most of the code,
  60.  including the MPW libraries, and the main program. Initialize contains
  61.  code that is only used once, during startup, and can be unloaded after the
  62.  program starts. %A5Init is automatically created by the Linker to initialize
  63.  globals for the MPW libraries and is unloaded right away.*)
  64.  
  65.  
  66. (*SetPort strategy:
  67.  
  68.  Toolbox routines do not change the current port. In spite of this, in this
  69.  program we use a strategy of calling SetPort whenever we want to draw or
  70.  make calls which depend on the current port. This makes us less vulnerable
  71.  to bugs in other software which might alter the current port (such as the
  72.  bug (feature?) in many desk accessories which change the port on OpenDeskAcc).
  73.  Hopefully, this also makes the routines from this program more self-contained,
  74.  since they don't depend on the current port setting.*)
  75.  
  76.  
  77. (*Clipboard strategy:
  78.  
  79.  This program does not maintain a private scrap. Whenever a cut, copy, or paste
  80.  occurs, we import/export from the public scrap to TextEdit's scrap right away,
  81.  using the TEToScrap and TEFromScrap routines. If we did use a private scrap,
  82.  the import/export would be in the activate/deactivate event and suspend/resume
  83.  event routines.*)
  84.  
  85. IMPORT SYSTEM, Types, Traps, Memory, Quickdraw, Windows, Events, TextEdit,
  86.        Controls, Dialogs, Fonts, Menus, OSUtils, ToolUtils, SegLoad,
  87.        Scrap, Desk, DiskInit, TextUtils;
  88.  
  89.  
  90. CONST
  91.     (*MPW 3.0 will include a Traps.p interface file that includes constants for trap numbers.
  92.      These constants are from that file.*)
  93.     (*1.02 - Uses Traps.p to obtain trap numbers.*)
  94.  
  95.     (*1.01 - changed constants to begin with 'k' for consistency, except for resource IDs*)
  96.     (*kTextMargin is the number of pixels we leave blank at the edge of the window.*)
  97.     kTextMargin                = 2;
  98.  
  99.     (*kMaxOpenDocuments is used to determine whether a new document can be opened
  100.      or created. We keep track of the number of open documents, and disable the
  101.      menu items that create a new document when the maximum is reached. If the
  102.      number of documents falls below the maximum, the items are enabled again.*)
  103.     kMaxOpenDocuments        = 1;
  104.     
  105.     (*kMaxDocWidth is an arbitrary number used to specify the width of the TERec's
  106.     destination rectangle so that word wrap and horizontal scrolling can be
  107.     demonstrated.*)
  108.     kMaxDocWidth            = 576;
  109.     
  110.     (*kMinDocDim is used to limit the minimum dimension of a window when GrowWindow
  111.     is called.*)
  112.     kMinDocDim                = 64;
  113.     
  114.     (*kControlInvisible is used to 'turn off' controls (i.e., cause the control not
  115.     to be redrawn as a result of some Control Manager call such as SetCtlValue)
  116.     by being put into the contrlVis field of the record. kControlVisible is used
  117.     the same way to 'turn on' the control.*)
  118.     kControlInvisible        = 0;
  119.     kControlVisible            = $FF;
  120.     
  121.     (*kScrollBarAdjust and kScrollBarWidth are used in calculating
  122.     values for control positioning and sizing.*)
  123.     kScrollbarWidth            = 16;
  124.     kScrollbarAdjust        = kScrollbarWidth - 1;
  125.     
  126.     (*kScrollTweek compensates for off-by-one requirements of the scrollbars
  127.      to have borders coincide with the growbox.*)
  128.     kScrollTweek            = 2;
  129.     
  130.     (*kCrChar is used to match with a carriage return when calculating the
  131.     number of lines in the TextEdit record. kDelChar is used to check for
  132.     delete in keyDowns.*)
  133.     kCRChar                    = 0DX;
  134.     kDelChar                = 08X;
  135.     
  136.     (*kButtonScroll is how many pixels to scroll horizontally when the button part
  137.     of the horizontal scrollbar is pressed.*)
  138.     kButtonScroll            = 4;
  139.     
  140.     (*kMaxTELength is an arbitrary number used to limit the length of text in the TERec
  141.     so that various errors won't occur from too many characters in the text.*)
  142.     kMaxTELength            = 32000;
  143.  
  144.     (*kSysEnvironsVersion is passed to SysEnvirons to tell it which version of the
  145.      SysEnvRec we understand.*)
  146.     kSysEnvironsVersion        = 1;
  147.  
  148.     (*kOSEvent is the event number of the suspend/resume and mouse-moved events sent
  149.      by MultiFinder. Once we determine that an event is an osEvent, we look at the
  150.      high byte of the message sent to determine which kind it is. To differentiate
  151.      suspend and resume events we check the resumeMask bit.*)
  152.     kOSEvent                = Events.app4Evt;    (*event used by MultiFinder*)
  153.     kSuspendResumeMessage    = 1;        (*high byte of suspend/resume event message*)
  154.     kResumeMask                = 1;        (*bit of message field for resume vs. suspend*)
  155.     kMouseMovedMessage        = $FA;        (*high byte of mouse-moved event message*)
  156.     kNoEvents                = 0;        (*no events mask*)
  157.  
  158.     (*1.01 - kMinHeap - This is the minimum result from the following
  159.      equation:
  160.             
  161.             ORD(GetApplLimit) - ORD(ApplicZone)
  162.             
  163.      for the application to run. It will insure that enough memory will
  164.      be around for reasonable-sized scraps, FKEYs, etc. to exist with the
  165.      application, and still give the application some 'breathing room'.
  166.      To derive this number, we ran under a MultiFinder partition that was
  167.      our requested minimum size, as given in the 'SIZE' resource.*)
  168.      
  169.     kMinHeap    = 29 * 1024;
  170.     
  171.     (*1.01 - kMinSpace - This is the minimum result from PurgeSpace, when called
  172.      at initialization time, for the application to run. This number acts
  173.      as a double-check to insure that there really is enough memory for the
  174.      application to run, including what has been taken up already by
  175.      pre-loaded resources, the scrap, code, and other sundry memory blocks.*)
  176.      
  177.     kMinSpace    = 20 * 1024;
  178.     
  179.     (*kExtremeNeg and kExtremePos are used to set up wide open rectangles and regions.*)
  180.     kExtremeNeg        = -32768;
  181.     kExtremePos        = 32767 - 1;    (*required for old region bug*)
  182.     
  183.     (*kTESlop provides some extra security when pre-flighting edit commands.*)
  184.     kTESlop            = 1024;
  185.  
  186.     (*kErrStrings is the resource ID for the error strings STR# resource.*)
  187.     kErrStrings        = 128;
  188.  
  189.     (*The following are indicies into STR# resources.*)
  190.     eWrongMachine    = 1;
  191.     eSmallSize        = 2;
  192.     eNoMemory        = 3;
  193.     eNoSpaceCut        = 4;
  194.     eNoCut            = 5;
  195.     eNoCopy            = 6;
  196.     eExceedPaste    = 7;
  197.     eNoSpacePaste    = 8;
  198.     eNoWindow        = 9;
  199.     eExceedChar        = 10;
  200.     eNoPaste        = 11;
  201.     
  202.     (*The following constants are all resource IDs, corresponding to resources in Sample.r.*)
  203.     rMenuBar    = 128;                    (*application's menu bar*)
  204.     rAboutAlert    = 128;                    (*about alert*)
  205.     rUserAlert    = 129;                    (*user error alert*)
  206.     rDocWindow    = 128;                    (*application's window*)
  207.     rVScroll    = 128;                    (*vertical scrollbar control*)
  208.     rHScroll    = 129;                    (*horizontal scrollbar control*)
  209.  
  210.     (*The following constants are used to identify menus and their items. The menu IDs
  211.      have an "m" prefix and the item numbers within each menu have an "i" prefix.*)
  212.     mApple        = 128;                    (*Apple menu*)
  213.     iAbout        = 1;
  214.  
  215.     mFile        = 129;                    (*File menu*)
  216.     iNew        = 1;
  217.     iClose        = 4;
  218.     iQuit        = 12;
  219.  
  220.     mEdit        = 130;                    (*Edit menu*)
  221.     iUndo        = 1;
  222.     iCut        = 3;
  223.     iCopy        = 4;
  224.     iPaste        = 5;
  225.     iClear        = 6;
  226.  
  227.     (*1.01 - kDITop and kDILeft are used to locate the Disk Initialization dialogs.*)
  228.     kDITop        = $0050;
  229.     kDILeft        = $0070;
  230.  
  231.  
  232. TYPE
  233.     (*A DocumentRecord contains the WindowRecord for one of our document windows,
  234.      as well as the TEHandle for the text we are editing. We have added fields to
  235.      hold the ControlHandles to the vertical and horizontal scrollbars and to hold
  236.      the address of the default clikLoop that gets attached to a TERec when you call
  237.      TEAutoView. Other document fields can be added to this record as needed. For
  238.      a similar example, see how the Window Manager and Dialog Manager add fields
  239.      after the GrafPort.*)
  240.     DocumentRecord    = RECORD(Windows.WindowRecord)
  241.         (*docWindow    : Windows.WindowRecord;*)
  242.         docTE        : TextEdit.TEHandle;
  243.         docVScroll    : Controls.ControlHandle;
  244.         docHScroll    : Controls.ControlHandle;
  245.         docClik        : TextEdit.ClikLoopProcPtr;
  246.     END;
  247.     DocumentPeek    = POINTER TO DocumentRecord;
  248.  
  249.  
  250. VAR
  251.     (*The "g" prefix is used to emphasize that a variable is global.*)
  252.  
  253.     (*GMac is used to hold the result of a SysEnvirons call. This makes
  254.      it convenient for any routine to check the environment. It is
  255.      global information, anyway.*)
  256.     gMac                : OSUtils.SysEnvRec;    (*set up by Initialize*)
  257.  
  258.     (*GHasWaitNextEvent is set at startup, and tells whether the WaitNextEvent
  259.      trap is available. If it is false, we know that we must call GetNextEvent.*)
  260.     gHasWaitNextEvent    : BOOLEAN;        (*set up by Initialize*)
  261.  
  262.     (*GInBackground is maintained by our OSEvent handling routines. Any part of
  263.      the program can check it to find out if it is currently in the background.*)
  264.     gInBackground        : BOOLEAN;        (*maintained by Initialize and DoEvent*)
  265.  
  266.     (*GNumDocuments is used to keep track of how many open documents there are
  267.      at any time. It is maintained by the routines that open and close documents.*)
  268.     gNumDocuments        : INTEGER;        (*maintained by Initialize, DoNew, and DoCloseWindow*)
  269.  
  270.     
  271. (*$S Initialize*)
  272. PROCEDURE TrapAvailable(tNumber: INTEGER; tType: OSUtils.TrapType): BOOLEAN;
  273.  
  274. (*Check to see if a given trap is implemented. This is only used by the
  275.  Initialize routine in this program, so we put it in the Initialize segment.
  276.  The recommended approach to see if a trap is implemented is to see if
  277.  the address of the trap routine is the same as the address of the
  278.  Unimplemented trap.*)
  279. (*1.02 - Needs to be called after call to SysEnvirons so that it can check
  280.  if a ToolTrap is out of range of a pre-MacII ROM.*)
  281.  
  282. BEGIN
  283.     IF (tType = OSUtils.ToolTrap) &
  284.         (gMac.machineType > OSUtils.envMachUnknown) &
  285.         (gMac.machineType < OSUtils.envMacII) THEN        (*it's a 512KE, Plus, or SE*)
  286.         tNumber := BAND(tNumber, $03FF);
  287.         IF tNumber > $01FF THEN                            (*which means the tool traps*)
  288.             tNumber := Traps._Unimplemented                    (*only go to $01FF*)
  289.         END
  290.     END;
  291.     RETURN OSUtils.NGetTrapAddress(tNumber, tType) #
  292.             OSUtils.GetTrapAddress(Traps._Unimplemented);
  293. END TrapAvailable;
  294.  
  295.  
  296. (*$S Main*)
  297. PROCEDURE IsDAWindow(window: Windows.WindowPtr): BOOLEAN;
  298.  
  299. (*Check if a window belongs to a desk accessory.*)
  300.  
  301. BEGIN
  302.     IF window = NIL THEN
  303.         RETURN FALSE
  304.     ELSE    (*DA windows have negative windowKinds*)
  305.         RETURN  window.windowKind < 0
  306.     END
  307. END IsDAWindow;
  308.  
  309.  
  310. (*$S Main*)
  311. PROCEDURE IsAppWindow(window: Windows.WindowPtr): BOOLEAN;
  312.  
  313. (*Check to see if a window belongs to the application. If the window pointer
  314.  passed was NIL, then it could not be an application window. WindowKinds
  315.  that are negative belong to the system and windowKinds less than userKind
  316.  are reserved by Apple except for windowKinds equal to dialogKind, which
  317.  mean it is a dialog.
  318.  1.02 - In order to reduce the chance of accidentally treating some window
  319.  as an AppWindow that shouldn't be, we'll only return true if the windowkind
  320.  is userKind. If you add different kinds of windows to Sample you'll need
  321.  to change how this all works.*)
  322.  
  323. BEGIN
  324.     IF window = NIL THEN
  325.         RETURN FALSE
  326.     ELSE    (*application windows have windowKinds = userKind (8)*)
  327.         RETURN window.windowKind = Windows.userKind
  328.     END
  329. END IsAppWindow;
  330.  
  331.  
  332. (*$S Main*)
  333. PROCEDURE AlertUser(error: INTEGER);
  334.  
  335. (*Display an alert that tells the user an error occurred, then exit the program.
  336.  This routine is used as an ultimate bail-out for serious errors that prohibit
  337.  the continuation of the application. Errors that do not require the termination
  338.  of the application should be handled in a different manner. Error checking and
  339.  reporting has a place even in the simplest application. The error number is used
  340.  to index an 'STR#' resource so that a relevant message can be displayed.*)
  341.  
  342. VAR
  343.     itemHit    : INTEGER;
  344.     message    : Types.Str255;
  345. BEGIN
  346.     Quickdraw.SetCursor(Quickdraw.arrow);
  347.     TextUtils.GetIndString(message, kErrStrings, error);
  348.     Dialogs.ParamText(message, "", "", "");
  349.     itemHit := Dialogs.Alert(rUserAlert, NIL);
  350. END AlertUser;
  351.  
  352.  
  353. (*$S Main*)
  354. PROCEDURE GetTERect(window: Windows.WindowPtr; VAR teRect: Types.Rect);
  355.  
  356. (*return a rectangle that is inset from the portRect by the size of
  357. the scrollbars and a little extra margin.*)
  358.  
  359. BEGIN
  360.     (*$TYPECHECK- type checks off*)
  361.     teRect := window.portRect;
  362.     (*$TYPECHECK+ type checks on*)
  363.     Quickdraw.InsetRect(teRect, kTextMargin, kTextMargin);            (*adjust for margin*)
  364.     DEC(teRect.botRight.v, kScrollbarAdjust);    (*and for the scrollbars*)
  365.     DEC(teRect.botRight.h, kScrollbarAdjust)
  366. END GetTERect;
  367.  
  368.  
  369. (*$S Main*)
  370. PROCEDURE DoCloseWindow(window: Windows.WindowPtr) : BOOLEAN;
  371.  
  372. (*Close a window. This handles desk accessory and application windows.*)
  373.  
  374. (*1.01 - At this point, if there was a document associated with a
  375.  window, you could do any document saving processing if it is 'dirty'.
  376.  DoCloseWindow would return TRUE if the window actually closed, i.e.,
  377.  the user didn’t cancel from a save dialog. This result is handy when
  378.  the user quits an application, but then cancels the save of a document
  379.  associated with a window.*)
  380.  
  381. BEGIN
  382.     IF IsDAWindow(window) THEN
  383.         Desk.CloseDeskAcc(window.windowKind)
  384.     ELSIF IsAppWindow(window) THEN
  385.         (*$TYPECHECK- type checks off*)
  386.         WITH window:DocumentPeek DO
  387.         (*$TYPECHECK+ type checks on*)
  388.             IF window.docTE # NIL THEN
  389.                 TextEdit.TEDispose(window.docTE);        (*dispose the TEHandle*)
  390.             END
  391.         END;
  392.         (*1.01 - We used to call DisposeWindow, but that was technically
  393.          incorrect, even though we allocated storage for the window on
  394.          the heap. We should instead call CloseWindow to have the structures
  395.          taken care of and then dispose of the storage ourselves.*)
  396.         Windows.CloseWindow(window);
  397.         Memory.DisposPtr(Types.Ptr(window));
  398.         DEC(gNumDocuments)
  399.     END;
  400.     RETURN TRUE
  401. END DoCloseWindow;
  402.  
  403.  
  404. (*$S Main*)
  405. PROCEDURE AdjustViewRect(docTE: TextEdit.TEHandle);
  406.  
  407. (*Update the TERec's view rect so that it is the greatest multiple of
  408. the lineHeight and still fits in the old viewRect.*)
  409.  
  410. BEGIN
  411.     docTE.viewRect.botRight.v := (((docTE.viewRect.botRight.v - docTE.viewRect.topLeft.v) DIV docTE.lineHeight) *
  412.                             docTE.lineHeight) + docTE.viewRect.topLeft.v;
  413. END AdjustViewRect;
  414.  
  415.  
  416. (*$S Main*)
  417. PROCEDURE AdjustTE(window: Windows.WindowPtr);
  418.  
  419. (*Scroll the TERec around to match up to the potentially updated scrollbar
  420. values. This is really useful when the window resizes such that the
  421. scrollbars become inactive and the TERec had been previously scrolled.*)
  422.  
  423. VAR
  424.     te        : TextEdit.TEHandle;
  425. BEGIN
  426.     (*$TYPECHECK- type checks off*)
  427.     WITH window:DocumentPeek DO
  428.     (*$TYPECHECK+ type checks on*)
  429.         te:=window.docTE;
  430.         TextEdit.TEScroll(
  431.             (te.viewRect.topLeft.h - te.destRect.topLeft.h)
  432.                 - Controls.GetCtlValue(window.docHScroll),
  433.             (te.viewRect.topLeft.v - te.destRect.topLeft.v)
  434.                 - (Controls.GetCtlValue(window.docVScroll) * te.lineHeight),
  435.             te)
  436.     END
  437. END AdjustTE;
  438.  
  439.  
  440. (*$S Main*)
  441. PROCEDURE AdjustHV(isVert: BOOLEAN; control: Controls.ControlHandle; docTE: TextEdit.TEHandle; canRedraw: BOOLEAN);
  442.  
  443. (*Calculate the new control maximum value and current value, whether it is the horizontal or
  444. vertical scrollbar. The vertical max is calculated by comparing the number of lines to the
  445. vertical size of the viewRect. The horizontal max is calculated by comparing the maximum document
  446. width to the width of the viewRect. The current values are set by comparing the offset between
  447. the view and destination rects. If necessary and we canRedraw, have the control be re-drawn by
  448. calling ShowControl.*)
  449.  
  450. VAR
  451.     value, lines, max    : INTEGER;
  452.     oldValue, oldMax    : INTEGER;
  453.     ch: CHAR;
  454. BEGIN
  455.     oldValue := Controls.GetCtlValue(control);
  456.     oldMax := Controls.GetCtlMax(control);
  457.     IF isVert THEN
  458.         lines := docTE.nLines;
  459.         (*since nLines isn’t right if the last character is a return, check for that case*)
  460.         SYSTEM.GET(LONGINT(MASTERPTR(docTE.hText))+docTE.teLength-1, ch);
  461.         IF ch = kCRChar THEN
  462.             lines := lines + 1
  463.         END;
  464.         max := lines - ((docTE.viewRect.botRight.v - docTE.viewRect.topLeft.v) DIV docTE.lineHeight);
  465.     ELSE
  466.         max := kMaxDocWidth - (docTE.viewRect.botRight.h - docTE.viewRect.topLeft.h);
  467.     END;
  468.     IF max < 0 THEN max := 0 END;            (*check for negative values*)
  469.     Controls.SetCtlMax(control, max);
  470.     IF isVert THEN
  471.         value := (docTE.viewRect.topLeft.v - docTE.destRect.topLeft.v) DIV docTE.lineHeight
  472.     ELSE
  473.         value := docTE.viewRect.topLeft.h - docTE.destRect.topLeft.h
  474.     END;
  475.     IF value < 0 THEN
  476.         value := 0
  477.     ELSIF value > max THEN
  478.         value := max                    (*pin the value to within range*)
  479.     END;
  480.     Controls.SetCtlValue(control, value);
  481.     IF canRedraw & ( (max # oldMax) OR (value # oldValue) ) THEN
  482.         Controls.ShowControl(control);            (*check to see if the control can be re-drawn*)
  483.     END
  484. END AdjustHV;
  485.  
  486.  
  487. (*$S Main*)
  488. PROCEDURE AdjustScrollValues(window: Windows.WindowPtr; canRedraw: BOOLEAN);
  489.  
  490. (*Simply call the common adjust routine for the vertical and horizontal scrollbars.*)
  491.  
  492. BEGIN
  493.     (*$TYPECHECK- type checks off*)
  494.     WITH window:DocumentPeek DO
  495.     (*$TYPECHECK+ type checks on*)
  496.         AdjustHV(TRUE, window.docVScroll, window.docTE, canRedraw);
  497.         AdjustHV(FALSE, window.docHScroll, window.docTE, canRedraw);
  498.     END
  499. END AdjustScrollValues;
  500.  
  501.  
  502. (*$S Main*)
  503. PROCEDURE AdjustScrollSizes(window: Windows.WindowPtr);
  504.  
  505. (*Re-calculate the position and size of the viewRect and the scrollbars.
  506.  kScrollTweek compensates for off-by-one requirements of the scrollbars
  507.  to have borders coincide with the growbox.*)
  508.  
  509. VAR
  510.     teRect    : Types.Rect;
  511. BEGIN
  512.     GetTERect(window, teRect);                                        (*start with teRect*)
  513.     (*$TYPECHECK- type checks off*)
  514.     WITH window:DocumentPeek DO
  515.     (*$TYPECHECK+ type checks on*)
  516.         window.docTE.viewRect := teRect;
  517.         AdjustViewRect(window.docTE);                                        (*snap to nearest line*)
  518.         Controls.MoveControl(window.docVScroll, window.portRect.botRight.h - kScrollbarAdjust, -1);
  519.         Controls.SizeControl(window.docVScroll, kScrollbarWidth, (window.portRect.botRight.v - window.portRect.botRight.v) -
  520.                         (kScrollbarAdjust - kScrollTweek));
  521.         Controls.MoveControl(window.docHScroll, -1, window.portRect.botRight.v - kScrollbarAdjust);
  522.         Controls.SizeControl(window.docHScroll, (window.portRect.botRight.h - window.portRect.topLeft.h) - (kScrollbarAdjust -
  523.                         kScrollTweek), kScrollbarWidth);
  524.     END
  525. END AdjustScrollSizes;
  526.  
  527.  
  528. (*$S Main*)
  529. PROCEDURE AdjustScrollbars(window: Windows.WindowPtr; needsResize: BOOLEAN);
  530.  
  531. (*Turn off the controls by jamming a zero into their contrlVis fields (HideControl erases them
  532. and we don't want that). If the controls are to be resized as well, call the procedure to do that,
  533. then call the procedure to adjust the maximum and current values. Finally re-enable the controls
  534. by jamming a $FF in their contrlVis fields.*)
  535.  
  536. BEGIN
  537.     (*$TYPECHECK- type checks off*)
  538.     WITH window:DocumentPeek DO
  539.     (*$TYPECHECK+ type checks on*)
  540.         (*First, turn visibility of scrollbars off so we won’t get unwanted redrawing*)
  541.         window.docVScroll.contrlVis := kControlInvisible;    (*turn them off*)
  542.         window.docHScroll.contrlVis := kControlInvisible;
  543.         IF needsResize THEN                                (*move and size if needed*)
  544.             AdjustScrollSizes(window)
  545.         END;
  546.         AdjustScrollValues(window, ¬needsResize);    (*fool with max and current value*)
  547.         (*Now, restore visibility in case we never had to ShowControl during adjustment*)
  548.         window.docVScroll.contrlVis := kControlVisible;        (*turn them on*)
  549.         window.docHScroll.contrlVis := kControlVisible;
  550.     END;
  551. END AdjustScrollbars;
  552.  
  553.  
  554. (*$S Main*)
  555. (*$Calling Pascal*)
  556. PROCEDURE PascalClikLoop*;
  557.  
  558. (*Gets called from our assembly language routine, AsmClikLoop, which is in
  559.  turn called by the TEClick toolbox routine. Saves the windows clip region,
  560.  sets it to the portRect, adjusts the scrollbar values to match the TE scroll
  561.  amount, then restores the clip region.*)
  562.  
  563. VAR
  564.     window    : Windows.WindowPtr;
  565.     region    : Quickdraw.RgnHandle;
  566. BEGIN
  567.     window := Windows.FrontWindow();
  568.     region := Quickdraw.NewRgn();
  569.     Quickdraw.GetClip(region);                    (*save the old clip*)
  570.     Quickdraw.ClipRect(window.portRect);            (*set the new clip*)
  571.     AdjustScrollValues(window, TRUE);    (*pass TRUE for canRedraw*)
  572.     Quickdraw.SetClip(region);                    (*restore the old clip*)
  573.     Quickdraw.DisposeRgn(region);
  574. END PascalClikLoop;
  575.  
  576.  
  577. (*$S Main*)
  578. PROCEDURE GetOldClikLoop*(): TextEdit.ClikLoopProcPtr;
  579.  
  580. (*Gets called from our assembly language routine, AsmClikLoop, which is in
  581. turn called by the TEClick toolbox routine. It returns the address of the
  582. default clikLoop routine that was put into the TERec by TEAutoView to
  583. AsmClikLoop so that it can call it.*)
  584.  
  585. VAR
  586.     window    : Windows.WindowPtr;
  587. BEGIN
  588.     window:=Windows.FrontWindow();
  589.     (*$TYPECHECK- type checks off*)
  590.     RETURN window(DocumentPeek).docClik
  591.     (*$TYPECHECK+ type checks on*)
  592. END GetOldClikLoop;
  593. (*$Calling Oberon*)
  594.  
  595.  
  596. PROCEDURE AsmClikLoop(): BOOLEAN;
  597.   EXTERNAL PASCAL;
  598.  
  599. (*A reference to our assembly language routine that gets attached to the clikLoop
  600. field of our TE record.*)
  601.  
  602.  
  603. (*$S Main*)
  604. PROCEDURE DoNew;
  605.  
  606. (*Create a new document and window.*)
  607.  
  608. VAR
  609.     good, ignore        : BOOLEAN;
  610.     storage                : Types.Ptr;
  611.     window                : Windows.WindowPtr;
  612.     destRect, viewRect    : Types.Rect;
  613.  
  614. BEGIN
  615.     storage := Memory.NewPtr(SIZE(DocumentRecord));
  616.     IF storage # NIL THEN
  617.         window := Windows.GetNewWindow(rDocWindow, storage, Windows.WindowPtr(-1));
  618.         IF window # NIL THEN
  619.             gNumDocuments := gNumDocuments + 1;    (*this will be decremented when we call DoCloseWindow*)
  620.             good := FALSE;
  621.             Quickdraw.SetPort(window);
  622.             (*$TYPECHECK- type checks off*)
  623.             WITH window:DocumentPeek DO
  624.             (*$TYPECHECK+ type checks on*)
  625.                 GetTERect(window, viewRect); destRect := viewRect;
  626.                 destRect.botRight.h := destRect.topLeft.h + kMaxDocWidth;
  627.                 window.docTE := TextEdit.TENew(destRect, viewRect);
  628.                 IF window.docTE # NIL THEN        (*1.02 - moved*)
  629.                     good := TRUE;                (*if TENew succeeded, we have a good document*)
  630.                     AdjustViewRect(window.docTE);
  631.                     TextEdit.TEAutoView(TRUE, window.docTE);
  632.                     window.docClik := window.docTE.clikLoop;
  633.                     window.docTE.clikLoop := AsmClikLoop;
  634.                 END;
  635.                 IF good THEN
  636.                     window.docVScroll := Controls.GetNewControl(rVScroll, window);
  637.                     good := window.docVScroll # NIL;
  638.                 END;
  639.                 IF good THEN
  640.                     window.docHScroll := Controls.GetNewControl(rHScroll, window);
  641.                     good := window.docHScroll # NIL;
  642.                 END;
  643.                 IF good THEN
  644.                     (*FALSE to AdjustScrollValues means musn’t redraw; technically, of course,
  645.                     the window is hidden so it wouldn’t matter whether we called ShowControl or not.*)
  646.                     AdjustScrollValues(window, FALSE);
  647.                     Windows.ShowWindow(window);            (*if the document is good, make the window visible*)
  648.                 ELSE
  649.                     ignore := DoCloseWindow(window);    (*otherwise regret we ever created it...*)
  650.                     AlertUser(eNoWindow);                (*and tell user*)
  651.                 END
  652.             END
  653.         ELSE
  654.             Memory.DisposPtr(storage)                    (*get rid of the storage if it is never used*)
  655.         END
  656.     END;
  657. END DoNew;
  658.  
  659.  
  660. (*$S Main*)
  661. PROCEDURE BigBadError(error: INTEGER);
  662. BEGIN
  663.     AlertUser(error);
  664.     SegLoad.ExitToShell;
  665. END BigBadError;
  666.  
  667.  
  668. (*$S Initialize*)
  669. PROCEDURE Initialize;
  670.  
  671. (*Set up the whole world, including global variables, Toolbox managers,
  672.  menus, and a single blank document.*)
  673.  
  674. (*1.01 - The code that used to be part of ForceEnvirons has been moved into
  675.  this module. If an error is detected, instead of merely doing an ExitToShell,
  676.  which leaves the user without much to go on, we call AlertUser, which puts
  677.  up a simple alert that just says an error occurred and then calls ExitToShell.
  678.  Since there is no other cleanup needed at this point if an error is detected,
  679.  this form of error- handling is acceptable. If more sophisticated error recovery
  680.  is needed, an exception mechanism, such as is provided by Signals, can be used.*)
  681.  
  682. VAR
  683.     menuBar                : Types.Handle;
  684.     total, contig        : LONGINT;
  685.     ignoreResult        : BOOLEAN;
  686.     event                : Events.EventRecord;
  687.     count, ignoreError    : INTEGER;
  688.     
  689. BEGIN
  690.     gInBackground := FALSE;
  691.  
  692.     Quickdraw.InitGraf(Quickdraw.thePort);
  693.     Fonts.InitFonts;
  694.     Windows.InitWindows;
  695.     Menus.InitMenus;
  696.     TextEdit.TEInit;
  697.     Dialogs.InitDialogs(NIL);
  698.     Quickdraw.InitCursor;
  699.  
  700.     (*Call MPPOpen and ATPLoad at this point to initialize AppleTalk,
  701.      if you are using it.*)
  702.     (*NOTE -- It is no longer necessary, and actually unhealthy, to check
  703.      PortBUse and SPConfig before opening AppleTalk. The drivers are capable
  704.      of checking for port availability themselves.*)
  705.     
  706.     (*This next bit of code is necessary to allow the default button of our
  707.      alert be outlined.
  708.      1.02 - Changed to call EventAvail so that we don't lose some important
  709.      events.*)
  710.      
  711.     FOR count := 1 TO 3 DO
  712.         ignoreResult := Events.EventAvail(Events.everyEvent, event)
  713.     END;
  714.     
  715.     (*Ignore the error returned from SysEnvirons; even if an error occurred,
  716.      the SysEnvirons glue will fill in the SysEnvRec. You can save a redundant
  717.      call to SysEnvirons by calling it after initializing AppleTalk.*)
  718.      
  719.     ignoreError := OSUtils.SysEnvirons(kSysEnvironsVersion, gMac);
  720.     
  721.     (*Make sure that the machine has at least 128K ROMs. If it doesn't, exit.*)
  722.     
  723.     IF gMac.machineType < 0 THEN BigBadError(eWrongMachine) END;
  724.     
  725.     (*1.02 - Move TrapAvailable call to after SysEnvirons so that we can tell
  726.      in TrapAvailable if a tool trap value is out of range.*)
  727.      
  728.     gHasWaitNextEvent := TrapAvailable(Traps._WaitNextEvent, OSUtils.ToolTrap);
  729.  
  730.     (*1.01 - We used to make a check for memory at this point by examining ApplLimit,
  731.      ApplicZone, and StackSpace and comparing that to the minimum size we told
  732.      MultiFinder we needed. This did not work well because it assumed too much about
  733.      the relationship between what we asked MultiFinder for and what we would actually
  734.      get back, as well as how to measure it. Instead, we will use an alternate
  735.      method comprised of two steps.*)
  736.      
  737.     (*It is better to first check the size of the application heap against a value
  738.      that you have determined is the smallest heap the application can reasonably
  739.      work in. This number should be derived by examining the size of the heap that
  740.      is actually provided by MultiFinder when the minimum size requested is used.
  741.      The derivation of the minimum size requested from MultiFinder is described
  742.      in Sample.h. The check should be made because the preferred size can end up
  743.      being set smaller than the minimum size by the user. This extra check acts to
  744.      insure that your application is starting from a solid memory foundation.*)
  745.      
  746.     IF LONGINT(Memory.GetApplLimit()) - LONGINT(Memory.ApplicZone()) < kMinHeap THEN BigBadError(eSmallSize) END;
  747.     
  748.     (*Next, make sure that enough memory is free for your application to run. It
  749.      is possible for a situation to arise where the heap may have been of required
  750.      size, but a large scrap was loaded which left too little memory. To check for
  751.      this, call PurgeSpace and compare the result with a value that you have determined
  752.      is the minimum amount of free memory your application needs at initialization.
  753.      This number can be derived several different ways. One way that is fairly
  754.      straightforward is to run the application in the minimum size configuration
  755.      as described previously. Call PurgeSpace at initialization and examine the value
  756.      returned. However, you should make sure that this result is not being modified
  757.      by the scrap's presence. You can do that by calling ZeroScrap before calling
  758.      PurgeSpace. Make sure to remove that call before shipping, though.*)
  759.      
  760.     (** ZeroScrap; **)
  761.     Memory.PurgeSpace(total, contig);
  762.     IF total < kMinSpace THEN
  763.         IF Scrap.UnloadScrap() # Types.noErr THEN
  764.             BigBadError(eNoMemory)
  765.         ELSE
  766.             Memory.PurgeSpace(total, contig);
  767.             IF total < kMinSpace THEN
  768.                 BigBadError(eNoMemory)
  769.             END
  770.         END
  771.     END;
  772.  
  773.     (*The extra benefit to waiting until after the Toolbox Managers have been initialized
  774.      to check memory is that we can now give the user an alert to tell her/him what
  775.      happened. Although it is possible that the memory situation could be worsened by
  776.      displaying an alert, MultiFinder would gracefully exit the application with
  777.      an informative alert if memory became critical. Here we are acting more
  778.      in a preventative manner to avoid future disaster from low-memory problems.*)
  779.  
  780.     menuBar := Menus.GetNewMBar(rMenuBar);        (*read menus into menu bar*)
  781.     IF menuBar = NIL THEN
  782.         BigBadError(eNoMemory)
  783.     END;
  784.     Menus.SetMenuBar(menuBar);                    (*install menus*)
  785.     Memory.DisposHandle(menuBar);
  786.     Menus.AddResMenu(Menus.GetMHandle(mApple), LONG("DRVR"));    (*add DA names to Apple menu*)
  787.     Menus.DrawMenuBar;
  788.  
  789.     gNumDocuments := 0;
  790.  
  791.     (*do other initialization here*)
  792.  
  793.     DoNew;                                    (*create a single empty document*)
  794. END Initialize;
  795.  
  796.  
  797. (**************************************************************************************
  798. 1.01 - PROCEDURE DoCloseBehind(window: WindowPtr); was removed.
  799.  
  800. (*1.01 - DoCloseBehind was a good idea for closing windows when quitting
  801.  and not having to worry about updating the windows, but it suffered
  802.  from a fatal flaw. If a desk accessory owned two windows, it would
  803.  close both those windows when CloseDeskAcc was called. When DoCloseBehind
  804.  got around to calling DoCloseWindow for that other window that was already
  805.  closed, things would go very poorly. Another option would be to have a
  806.  procedure, GetRearWindow, that would go through the window list and return
  807.  the last window. Instead, we decided to present the standard approach
  808.  of getting and closing FrontWindow until FrontWindow returns NIL. This
  809.  has a potential benefit in that the window whose document needs to be saved
  810.  may be visible since it is the front window, therefore decreasing the
  811.  chance of user confusion. For aesthetic reasons, the windows in the
  812.  application should be checked for updates periodically and have the
  813.  updates serviced.*)
  814. **************************************************************************************)
  815.  
  816.  
  817. (*$S Main*)
  818. PROCEDURE Terminate;
  819.  
  820. (*Clean up the application and exit. We close all of the windows so that
  821.  they can update their documents, if any.*)
  822.  
  823. (*1.01 - If we find out that a cancel has occurred, we won't exit to the
  824.  shell, but will return instead.*)
  825.  
  826. VAR
  827.     aWindow    : Windows.WindowPtr;
  828.     closed    : BOOLEAN;
  829.  
  830. BEGIN
  831.     closed := TRUE;
  832.     REPEAT
  833.         aWindow := Windows.FrontWindow();                    (*get the current front window*)
  834.         IF aWindow # NIL THEN
  835.             closed := DoCloseWindow(aWindow);    (*close this window*)
  836.         END
  837.     UNTIL (¬closed) OR (aWindow = NIL);        (*do all windows*)
  838.     IF closed THEN
  839.         SegLoad.ExitToShell                            (*exit if no cancellation*)
  840.     END
  841. END Terminate;
  842.  
  843.  
  844. (*$S Main*)
  845. PROCEDURE AdjustMenus;
  846.  
  847. (*Enable and disable menus based on the current state.
  848.  The user can only select enabled menu items. We set up all the menu items
  849.  before calling MenuSelect or MenuKey, since these are the only times that
  850.  a menu item can be selected. Note that MenuSelect is also the only time
  851.  the user will see menu items. This approach to deciding what enable/
  852.  disable state a menu item has the advantage of concentrating all the decision-
  853.  making in one routine, as opposed to being spread throughout the application.
  854.  Other application designs may take a different approach that may or may not be
  855.  as valid.*)
  856.  
  857. VAR
  858.     window            : Windows.WindowPtr;
  859.     menu            : Menus.MenuHandle;
  860.     offset            : LONGINT;
  861.     undo            : BOOLEAN;
  862.     cutCopyClear    : BOOLEAN;
  863.     paste            : BOOLEAN;
  864.  
  865. BEGIN
  866.     window := Windows.FrontWindow();
  867.  
  868.     menu := Menus.GetMHandle(mFile);
  869.     IF gNumDocuments < kMaxOpenDocuments THEN
  870.         Menus.EnableItem(menu, iNew)        (*New is enabled when we can open more documents*)
  871.     ELSE
  872.         Menus.DisableItem(menu, iNew)
  873.     END;
  874.     IF window # NIL THEN            (*Close is enabled when there is a window to close*)
  875.         Menus.EnableItem(menu, iClose)
  876.     ELSE
  877.         Menus.DisableItem(menu, iClose)
  878.     END;
  879.  
  880.     menu := Menus.GetMHandle(mEdit);
  881.     undo := FALSE;
  882.     cutCopyClear := FALSE;
  883.     paste := FALSE;
  884.     IF IsDAWindow(window) THEN
  885.         undo := TRUE;                (*all editing is enabled for DA windows*)
  886.         cutCopyClear := TRUE;
  887.         paste := TRUE;
  888.     ELSIF IsAppWindow(window) THEN
  889.         (*$TYPECHECK- type checks off*)
  890.         WITH window:DocumentPeek DO
  891.         (*$TYPECHECK+ type checks on*)
  892.             IF window.docTE.selStart < window.docTE.selEnd THEN
  893.                 cutCopyClear := TRUE
  894.             END
  895.         END;
  896.                 (*Cut, Copy, and Clear is enabled for app. windows with selections*)
  897.         IF Scrap.GetScrap(NIL, LONG("TEXT"), offset) > 0 THEN
  898.             paste := TRUE            (*Paste is enabled for app. windows*)
  899.         END
  900.     END;
  901.     IF undo THEN
  902.         Menus.EnableItem(menu, iUndo)
  903.     ELSE
  904.         Menus.DisableItem(menu, iUndo)
  905.     END;
  906.     IF cutCopyClear THEN
  907.         Menus.EnableItem(menu, iCut);
  908.         Menus.EnableItem(menu, iCopy);
  909.         Menus.EnableItem(menu, iClear);
  910.     ELSE
  911.         Menus.DisableItem(menu, iCut);
  912.         Menus.DisableItem(menu, iCopy);
  913.         Menus.DisableItem(menu, iClear);
  914.     END;
  915.     IF paste THEN
  916.         Menus.EnableItem(menu, iPaste)
  917.     ELSE
  918.         Menus.DisableItem(menu, iPaste)
  919.     END;
  920. END AdjustMenus;
  921.  
  922.  
  923. (*$S Main*)
  924. PROCEDURE DoMenuCommand(menuResult: LONGINT);
  925.  
  926. (*This is called when an item is chosen from the menu bar (after calling
  927.  MenuSelect or MenuKey). It does the right thing for each command.*)
  928.  
  929. VAR
  930.     menuID, menuItem        : INTEGER;
  931.     itemHit, daRefNum        : INTEGER;
  932.     daName                    : Types.Str255;
  933.     ignoreResult, saveErr    : Types.OSErr;
  934.     te                        : TextEdit.TEHandle;
  935.     window                    : Windows.WindowPtr;
  936.     ignore                    : BOOLEAN;
  937.     aHandle                    : Types.Handle;
  938.     oldSize, newSize        : LONGINT;
  939.     total, contig            : LONGINT;
  940.  
  941. BEGIN
  942.     window := Windows.FrontWindow();
  943.     menuID := HiWrd(menuResult);    (*use built-ins (for efficiency)...*)
  944.     menuItem := LoWrd(menuResult);    (*to get menu item number and menu number*)
  945.     CASE menuID OF
  946.     
  947.         mApple:
  948.             IF menuItem=iAbout THEN                (*bring up alert for About*)
  949.                     itemHit := Dialogs.Alert(rAboutAlert, NIL);
  950.             ELSE        (*all non-About items in this menu are DAs*)
  951.                 Menus.GetItem(Menus.GetMHandle(mApple), menuItem, daName);
  952.                 daRefNum := Desk.OpenDeskAcc(daName);
  953.             END|
  954.             
  955.         mFile:
  956.             CASE menuItem OF
  957.                 iNew:
  958.                     DoNew|
  959.                 iClose:
  960.                     ignore := DoCloseWindow(window)| (*we don't care if cancelled*)
  961.                 iQuit:
  962.                     Terminate|
  963.                 ELSE (* ignore other items *)
  964.             END|
  965.             
  966.         mEdit:                (*call SystemEdit for DA editing & MultiFinder*)
  967.             IF ¬Desk.SystemEdit(menuItem-1) THEN
  968.                 (*$TYPECHECK- type checks off*)
  969.                 te := window(DocumentPeek).docTE;
  970.                 (*$TYPECHECK+ type checks on*)
  971.                 CASE menuItem OF
  972.                 
  973.                     iCut:        (*after cutting, export the TE scrap*)
  974.                         IF Scrap.ZeroScrap() = Types.noErr THEN
  975.                             Memory.PurgeSpace(total, contig);
  976.                             IF (te.selEnd - te.selStart) + kTESlop > contig THEN
  977.                                 AlertUser(eNoSpaceCut)
  978.                             ELSE
  979.                                 TextEdit.TECut(te);
  980.                                 IF TextEdit.TEToScrap() # Types.noErr THEN
  981.                                     AlertUser(eNoCut);
  982.                                     ignoreResult := SHORT(Scrap.ZeroScrap())
  983.                                 END
  984.                             END
  985.                         END|
  986.                     
  987.                     iCopy:    (*after copying, export the TE scrap*)
  988.                         IF Scrap.ZeroScrap() = Types.noErr THEN
  989.                             TextEdit.TECopy(te);
  990.                             IF TextEdit.TEToScrap() # Types.noErr THEN
  991.                                 AlertUser(eNoCopy);
  992.                                 ignoreResult := SHORT(Scrap.ZeroScrap())
  993.                             END
  994.                         END|
  995.                     
  996.                     iPaste:    (*import the TE scrap before pasting*)
  997.                         IF TextEdit.TEFromScrap() = Types.noErr THEN
  998.                             IF TextEdit.TEGetScrapLen() + (te.teLength -
  999.                                 (te.selEnd - te.selStart)) > kMaxTELength THEN
  1000.                                 AlertUser(eExceedPaste)
  1001.                             ELSE
  1002.                                 aHandle := Types.Handle(TextEdit.TEGetText(te));
  1003.                                 oldSize := Memory.GetHandleSize(aHandle);
  1004.                                 newSize := oldSize + TextEdit.TEGetScrapLen() + kTESlop;
  1005.                                 Memory.SetHandleSize(aHandle, newSize);
  1006.                                 saveErr := Memory.MemError();
  1007.                                 Memory.SetHandleSize(aHandle, oldSize);
  1008.                                 IF saveErr # Types.noErr THEN
  1009.                                     AlertUser(eNoSpacePaste)
  1010.                                 ELSE
  1011.                                     TextEdit.TEPaste(te)
  1012.                                 END
  1013.                             END
  1014.                         ELSE
  1015.                             AlertUser(eNoPaste)
  1016.                         END|
  1017.                     
  1018.                     iClear:
  1019.                         TextEdit.TEDelete(te)|
  1020.                     ELSE (*ignore other otems *)
  1021.                         
  1022.                 END;
  1023.                 AdjustScrollbars(window, FALSE);
  1024.                 AdjustTE(window);
  1025.             END|
  1026.         ELSE (* ignore other menus *)
  1027.     END;
  1028.     Menus.HiliteMenu(0)                    (*unhighlight what MenuSelect (or MenuKey) hilited*)
  1029. END DoMenuCommand;
  1030.  
  1031.  
  1032. (*$S Main*)
  1033. PROCEDURE DrawWindow(window: Windows.WindowPtr);
  1034.  
  1035. (*Draw the contents of an application window.*)
  1036.  
  1037. BEGIN
  1038.     Quickdraw.SetPort(window);
  1039.     Quickdraw.EraseRect(window.portRect);        (*as per TextEdit chapter of Inside Macintosh*)
  1040.     Controls.DrawControls(window);        (*this ordering makes for a better appearance*)
  1041.     Windows.DrawGrowIcon(window);
  1042.     (*$TYPECHECK- type checks off*)
  1043.     TextEdit.TEUpdate(window.portRect, window(DocumentPeek).docTE)
  1044.     (*$TYPECHECK+ type checks on*)
  1045. END DrawWindow;
  1046.  
  1047.  
  1048. (*$S Main*)
  1049. PROCEDURE GetSleep(): LONGINT;
  1050.  
  1051. (*Calculate a sleep value for WaitNextEvent. This takes into account the things
  1052.  that DoIdle does with idle time.*)
  1053.  
  1054. VAR
  1055.     sleep    : LONGINT;
  1056.     window    : Windows.WindowPtr;
  1057.  
  1058. BEGIN
  1059.     sleep := MAX(LONGINT);                    (*default value for sleep*)
  1060.     IF ¬gInBackground THEN            (*if we are in front...*)
  1061.         window := Windows.FrontWindow();            (*and the front window is ours...*)
  1062.         IF IsAppWindow(window) THEN
  1063.             (*$TYPECHECK- type checks off*)
  1064.             WITH window:DocumentPeek DO
  1065.             (*$TYPECHECK+ type checks on*)
  1066.                 IF window.docTE.selStart = window.docTE.selEnd THEN    (*and the selection is an insertion point...*)
  1067.                     sleep := Events.GetCaretTime()    (*we need to blink the insertion point*)
  1068.                 END
  1069.             END
  1070.         END
  1071.     END;
  1072.     RETURN sleep
  1073. END GetSleep;
  1074.  
  1075.  
  1076. (*$S Main*)
  1077. PROCEDURE CommonAction(control: Controls.ControlHandle; VAR amount: INTEGER);
  1078.  
  1079. (*Common algorithm for setting the new value of a control. It returns the actual amount
  1080. the value of the control changed. Note the pinning is done for the sake of returning
  1081. the amount the control value changed.*)
  1082.  
  1083. VAR
  1084.     value, max    : INTEGER;
  1085. BEGIN
  1086.     value := Controls.GetCtlValue(control);    (*get current value*)
  1087.     max := Controls.GetCtlMax(control);        (*and max value*)
  1088.     amount := value - amount;
  1089.     IF amount < 0 THEN
  1090.         amount := 0
  1091.     ELSIF amount > max THEN
  1092.         amount := max
  1093.     END;
  1094.     Controls.SetCtlValue(control, amount);
  1095.     amount := value - amount;        (*calculate true change*)
  1096. END CommonAction;
  1097.  
  1098.  
  1099. (*$S Main*)
  1100. (*$Calling Pascal*)
  1101. PROCEDURE VActionProc(control: Controls.ControlHandle; part: INTEGER);
  1102.  
  1103. (*Determines how much to change the value of the vertical scrollbar by and how
  1104. much to scroll the TE record.*)
  1105.  
  1106. VAR
  1107.     amount    : INTEGER;
  1108.     window    : Windows.WindowPtr;
  1109. BEGIN
  1110.     IF part # 0 THEN
  1111.         window := Windows.WindowPtr(control.contrlOwner);
  1112.         (*$TYPECHECK- type checks off*)
  1113.         WITH window:DocumentPeek DO
  1114.         (*$TYPECHECK+ type checks on*)
  1115.             CASE part OF
  1116.                 Controls.inUpButton, Controls.inDownButton:
  1117.                     amount := 1|                                                (*one line*)
  1118.                 Controls.inPageUp, Controls.inPageDown:
  1119.                     amount := (window.docTE.viewRect.botRight.v - window.docTE.viewRect.topLeft.v) DIV window.docTE.lineHeight|    (*one page*)
  1120.                 ELSE (* ignore other parts *)
  1121.             END;
  1122.             IF (part = Controls.inDownButton) OR (part = Controls.inPageDown) THEN
  1123.                 amount := -amount                                                (*reverse direction*)
  1124.             END;
  1125.             CommonAction(control, amount);
  1126.             IF amount # 0 THEN
  1127.                 TextEdit.TEScroll(0, amount * window.docTE.lineHeight, window.docTE)
  1128.             END
  1129.         END
  1130.     END
  1131. END VActionProc;
  1132.  
  1133.  
  1134. (*$S Main*)
  1135. PROCEDURE HActionProc(control: Controls.ControlHandle; part: INTEGER);
  1136.  
  1137. (*Determines how much to change the value of the horizontal scrollbar by and how
  1138. much to scroll the TE record.*)
  1139.  
  1140. VAR
  1141.     amount    : INTEGER;
  1142.     window    : Windows.WindowPtr;
  1143. BEGIN
  1144.     IF part # 0 THEN
  1145.         window := Windows.WindowPtr(control.contrlOwner);
  1146.         (*$TYPECHECK- type checks off*)
  1147.         WITH window:DocumentPeek DO
  1148.         (*$TYPECHECK+ type checks on*)
  1149.             CASE part OF
  1150.                 Controls.inUpButton, Controls.inDownButton:                                        (*a few pixels*)
  1151.                     amount := kButtonScroll|
  1152.                 Controls.inPageUp, Controls.inPageDown:
  1153.                     amount := window.docTE.viewRect.botRight.h - window.docTE.viewRect.topLeft.h|                    (*a page*)
  1154.                 ELSE (* ignore other parts *)
  1155.             END;
  1156.             IF (part = Controls.inDownButton) OR (part = Controls.inPageDown) THEN
  1157.                 amount := -amount;                                                (*reverse direction*)
  1158.             END;
  1159.             CommonAction(control, amount);
  1160.             IF amount # 0 THEN
  1161.                 TextEdit.TEScroll(amount, 0, window.docTE)
  1162.             END
  1163.         END
  1164.     END
  1165. END HActionProc;
  1166. (*$Calling Oberon*)
  1167.  
  1168.  
  1169. (*$S Main*)
  1170. PROCEDURE DoIdle;
  1171.  
  1172. (*This is called whenever we get an null event or a mouse-moved event.
  1173.  It takes care of necessary periodic actions. For this program, it calls TEIdle.*)
  1174.  
  1175. VAR
  1176.     window    : Windows.WindowPtr;
  1177.  
  1178. BEGIN
  1179.     window := Windows.FrontWindow();
  1180.     IF IsAppWindow(window) THEN
  1181.         (*$TYPECHECK- type checks off*)
  1182.         TextEdit.TEIdle(window(DocumentPeek).docTE)
  1183.         (*$TYPECHECK+ type checks on*)
  1184.     END
  1185. END DoIdle;
  1186.  
  1187.  
  1188. (*$S Main*)
  1189. PROCEDURE DoKeyDown(event: Events.EventRecord);
  1190.  
  1191. (*This is called for any keyDown or autoKey events, except when the
  1192.  Command key is held down. It looks at the frontmost window to decide what
  1193.  to do with the key typed.*)
  1194.  
  1195. VAR
  1196.     window    : Windows.WindowPtr;
  1197.     key        : CHAR;
  1198.     te        : TextEdit.TEHandle;
  1199.  
  1200. BEGIN
  1201.     window := Windows.FrontWindow();
  1202.     IF IsAppWindow(window) THEN
  1203.         (*$TYPECHECK- type checks off*)
  1204.         te := window(DocumentPeek).docTE;
  1205.         (*$TYPECHECK+ type checks on*)
  1206.         key := CHR(BAND(event.message, Events.charCodeMask));
  1207.         IF (key = kDelChar) OR                            (*don't count deletes*)
  1208.             (te.teLength - (te.selEnd - te.selStart)
  1209.                             + 1 < kMaxTELength) THEN    (*but check haven't gone past*)
  1210.             TextEdit.TEKey(key, te);
  1211.             AdjustScrollbars(window, FALSE);
  1212.             AdjustTE(window);
  1213.         ELSE
  1214.             AlertUser(eExceedChar)
  1215.         END
  1216.     END
  1217. END DoKeyDown;
  1218.  
  1219.  
  1220. (*$S Main*)
  1221. PROCEDURE DoContentClick(window: Windows.WindowPtr; event: Events.EventRecord);
  1222.  
  1223. (*Called when a mouseDown occurs in the content of a window.*)
  1224.  
  1225. VAR
  1226.     mouse        : Types.Point;
  1227.     control        : Controls.ControlHandle;
  1228.     part, value    : INTEGER;
  1229.     shiftDown    : BOOLEAN;
  1230.     teRect        : Types.Rect;
  1231.  
  1232. BEGIN
  1233.     IF IsAppWindow(window) THEN
  1234.         Quickdraw.SetPort(window);
  1235.         mouse := event.where;                                            (*get the click position*)
  1236.         Quickdraw.GlobalToLocal(mouse);                                            (*convert to local coordinates*)
  1237.         (*see if we are in the viewRect. if so, we won’t check the controls*)
  1238.         GetTERect(window, teRect);
  1239.         IF Quickdraw.PtInRect(mouse, teRect) THEN
  1240.             shiftDown := BAND(event.modifiers, Events.shiftKey) # 0;    (*extend if Shift is down*)
  1241.             (*$TYPECHECK- type checks off*)
  1242.             TextEdit.TEClick(mouse, shiftDown, window(DocumentPeek).docTE);
  1243.             (*$TYPECHECK+ type checks on*)
  1244.         ELSE
  1245.             part := Controls.FindControl(mouse, window, control);
  1246.             (*$TYPECHECK- type checks off*)
  1247.             WITH window:DocumentPeek DO
  1248.             (*$TYPECHECK+ type checks on*)
  1249.                 CASE part OF
  1250.                     0:|                                            (*do nothing for viewRect case*)
  1251.                     Controls.inThumb:
  1252.                         value := Controls.GetCtlValue(control);
  1253.                         part := Controls.TrackControl(control, mouse, NIL);
  1254.                         IF part # 0 THEN
  1255.                             value := value - Controls.GetCtlValue(control);
  1256.                             IF value # 0 THEN
  1257.                                 IF control = window.docVScroll THEN
  1258.                                     TextEdit.TEScroll(0, value * window.docTE.lineHeight, window.docTE)
  1259.                                 ELSE
  1260.                                     TextEdit.TEScroll(value, 0, window.docTE)
  1261.                                 END
  1262.                             END
  1263.                         END|
  1264.                     ELSE                                    (*must be page or button*)
  1265.                         IF control = window.docVScroll THEN
  1266.                             value := Controls.TrackControl(control, mouse, SYSTEM.ADR(VActionProc))
  1267.                         ELSE
  1268.                             value := Controls.TrackControl(control, mouse, SYSTEM.ADR(HActionProc))
  1269.                         END
  1270.                 END
  1271.             END
  1272.         END
  1273.     END
  1274. END DoContentClick;
  1275.  
  1276.  
  1277. (*$S Main*)
  1278. PROCEDURE ResizeWindow(window: Windows.WindowPtr);
  1279.  
  1280. (*Called when the window has been resized to fix up the controls and content*)
  1281.  
  1282. BEGIN
  1283.     AdjustScrollbars(window, TRUE);
  1284.     AdjustTE(window);
  1285.     Windows.InvalRect(window.portRect);
  1286. END ResizeWindow;
  1287.  
  1288.  
  1289. (*$S Main*)
  1290. PROCEDURE GetLocalUpdateRgn(window: Windows.WindowPtr; localRgn: Quickdraw.RgnHandle);
  1291.  
  1292. (*Returns the update region in local coordinates*)
  1293.  
  1294. BEGIN
  1295.     Quickdraw.CopyRgn(window.updateRgn, localRgn);                        (*save old update region*)
  1296.     Quickdraw.OffsetRgn(localRgn, window.portBits.bounds.topLeft.h, window.portBits.bounds.topLeft.v);                                        (*convert to local coords*)
  1297. END GetLocalUpdateRgn;
  1298.  
  1299.  
  1300. (*$S Main*)
  1301. PROCEDURE DoGrowWindow(window: Windows.WindowPtr; event: Events.EventRecord);
  1302.  
  1303. (*Called when a mouseDown occurs in the grow box of an active window. In
  1304.  order to eliminate any 'flicker', we want to invalidate only what is
  1305.  necessary. Since ResizeWindow invalidates the whole portRect, we save
  1306.  the old TE viewRect, intersect it with the new TE viewRect, and
  1307.  remove the result from the update region. However, we must make sure
  1308.  that any old update region that might have been around gets put back.*)
  1309.  
  1310. VAR
  1311.     growResult        : LONGINT;
  1312.     tempRect        : Types.Rect;
  1313.     tempRgn            : Quickdraw.RgnHandle;
  1314.     ignoreResult    : BOOLEAN;
  1315.  
  1316. BEGIN
  1317.     Quickdraw.SetRect(tempRect, kMinDocDim, kMinDocDim,
  1318.                       Quickdraw.screenBits.bounds.botRight.h,
  1319.                       Quickdraw.screenBits.bounds.botRight.v);            (*set up limiting values*)
  1320.     growResult := Windows.GrowWindow(window, event.where, tempRect);
  1321.     IF growResult # 0 THEN                                                 (*see if changed size*)
  1322.         (*$TYPECHECK- type checks off*)
  1323.         WITH window:DocumentPeek DO
  1324.         (*$TYPECHECK+ type checks on*)
  1325.             tempRect := window.docTE.viewRect;                                    (*save old text box*)
  1326.             tempRgn := Quickdraw.NewRgn();
  1327.             GetLocalUpdateRgn(window, tempRgn);                                (*get localized update region*)
  1328.             Windows.SizeWindow(window, LoWrd(growResult), HiWrd(growResult), TRUE);
  1329.             ResizeWindow(window);
  1330.             ignoreResult := Quickdraw.SectRect(tempRect, window.docTE.viewRect, tempRect);    (*find what stayed same*)
  1331.             Windows.ValidRect(tempRect);                                            (*take it out of update*)
  1332.             Windows.InvalRgn(tempRgn);                                                (*put back any prior update*)
  1333.             Quickdraw.DisposeRgn(tempRgn);
  1334.         END
  1335.     END
  1336. END DoGrowWindow;
  1337.  
  1338.  
  1339. (*$S Main*)
  1340. PROCEDURE DoZoomWindow(window: Windows.WindowPtr; part: INTEGER);
  1341.  
  1342. (*Called when a mouseClick occurs in the zoom box of an active window.
  1343.  Everything has to get re-drawn here, so we don't mind that
  1344.  ResizeWindow invalidates the whole portRect.*)
  1345.  
  1346. BEGIN
  1347.     Quickdraw.EraseRect(window.portRect);
  1348.     Windows.ZoomWindow(window, part, window = Windows.FrontWindow());
  1349.     ResizeWindow(window);
  1350. END DoZoomWindow;
  1351.  
  1352.  
  1353. (*$S Main*)
  1354. PROCEDURE DoUpdate(window: Windows.WindowPtr);
  1355.  
  1356. (*This is called when an update event is received for a window.
  1357.  It calls DrawWindow to draw the contents of an application window,
  1358.  but only if the visRgn is non-empty; for efficiency reasons,
  1359.  not because it is required.*)
  1360.  
  1361. BEGIN
  1362.     IF IsAppWindow(window) THEN
  1363.         Windows.BeginUpdate(window);                    (*this sets up the visRgn*)
  1364.         IF ¬Quickdraw.EmptyRgn(window.visRgn) THEN    (*draw if updating needs to be done*)
  1365.             DrawWindow(window);
  1366.         END;
  1367.         Windows.EndUpdate(window)
  1368.     END
  1369. END DoUpdate;
  1370.  
  1371.  
  1372. (*$S Main*)
  1373. PROCEDURE DoActivate(window: Windows.WindowPtr; becomingActive: BOOLEAN);
  1374.  
  1375. (*This is called when a window is activated or deactivated.*)
  1376.  
  1377. VAR
  1378.     tempRgn, clipRgn    : Quickdraw.RgnHandle;
  1379.     growRect            : Types.Rect;
  1380.  
  1381. BEGIN
  1382.     IF IsAppWindow(window) THEN
  1383.         (*$TYPECHECK- type checks off*)
  1384.         WITH window:DocumentPeek DO
  1385.         (*$TYPECHECK+ type checks on*)
  1386.             IF becomingActive THEN
  1387.                 (*since we don’t want TEActivate to draw a selection in an area where
  1388.                  we’re going to erase and redraw, we’ll clip out the update region
  1389.                  before calling it.*)
  1390.                 tempRgn := Quickdraw.NewRgn();
  1391.                 clipRgn := Quickdraw.NewRgn();
  1392.                 GetLocalUpdateRgn(window, tempRgn);            (*get localized update region*)
  1393.                 Quickdraw.GetClip(clipRgn);
  1394.                 Quickdraw.DiffRgn(clipRgn, tempRgn, tempRgn);            (*subtract updateRgn from clipRgn*)
  1395.                 Quickdraw.SetClip(tempRgn);
  1396.                 TextEdit.TEActivate(window.docTE);                            (*let TE do its thing*)
  1397.                 Quickdraw.SetClip(clipRgn);                            (*restore the full-blown clipRgn*)
  1398.                 Quickdraw.DisposeRgn(tempRgn);
  1399.                 Quickdraw.DisposeRgn(clipRgn);
  1400.  
  1401.                 (*the controls need to be redrawn on activation:*)
  1402.                 window.docVScroll.contrlVis := kControlVisible;
  1403.                 window.docHScroll.contrlVis := kControlVisible;
  1404.                 Windows.InvalRect(window.docVScroll.contrlRect);
  1405.                 Windows.InvalRect(window.docHScroll.contrlRect);
  1406.                 (*the growbox needs to be redrawn on activation:*)
  1407.                 growRect := window.portRect;
  1408.                 growRect.topLeft.v := growRect.botRight.v - kScrollbarAdjust;        (*adjust for the scrollbars*)
  1409.                 growRect.topLeft.h := growRect.botRight.h - kScrollbarAdjust;
  1410.                 Windows.InvalRect(growRect);
  1411.             ELSE
  1412.                 TextEdit.TEDeactivate(window.docTE);
  1413.                 (*the controls should be hidden immediately on deactivation:*)
  1414.                 Controls.HideControl(window.docVScroll);
  1415.                 Controls.HideControl(window.docHScroll);
  1416.                 (*the growbox should be changed immediately on deactivation:*)
  1417.                 Windows.DrawGrowIcon(window);
  1418.             END
  1419.         END
  1420.     END
  1421. END DoActivate;
  1422.  
  1423.  
  1424. (*$S Main*)
  1425. PROCEDURE GetGlobalMouse(VAR mouse: Types.Point);
  1426.  
  1427. (*Get the global coordinates of the mouse. When you call OSEventAvail
  1428.  it will return either a pending event or a null event. In either case,
  1429.  the where field of the event record will contain the current position
  1430.  of the mouse in global coordinates and the modifiers field will reflect
  1431.  the current state of the modifiers. Another way to get the global
  1432.  coordinates is to call GetMouse and LocalToGlobal, but that requires
  1433.  being sure that thePort is set to a valid port.*)
  1434.  
  1435. VAR
  1436.     event    : Events.EventRecord;
  1437.     
  1438. BEGIN
  1439.     IF Events.OSEventAvail(kNoEvents, event) THEN END;    (*we aren't interested in any events*)
  1440.     (*$TYPECHECK- type checks off*)
  1441.     mouse := event.where;                    (*just the mouse position*)
  1442.     (*$TYPECHECK+ type checks on*)
  1443. END GetGlobalMouse;
  1444.  
  1445.  
  1446. (*$S Main*)
  1447. PROCEDURE AdjustCursor(mouse: Types.Point; region: Quickdraw.RgnHandle);
  1448.  
  1449. (*Change the cursor's shape, depending on its position. This also calculates the region
  1450.  where the current cursor resides (for WaitNextEvent). If the mouse is ever outside of
  1451.  that region, an event is generated, causing this routine to be called. This
  1452.  allows us to change the region to the region the mouse is currently in. If
  1453.  there is more to the event than just “the mouse moved”, we get called before the
  1454.  event is processed to make sure the cursor is the right one. In any (ahem) event,
  1455.  this is called again before we fall back into WNE.*)
  1456.  
  1457.  
  1458. VAR
  1459.     window        : Windows.WindowPtr;
  1460.     arrowRgn    : Quickdraw.RgnHandle;
  1461.     iBeamRgn    : Quickdraw.RgnHandle;
  1462.     iBeamRect    : Types.Rect;
  1463.     aCursor        : Quickdraw.CursHandle;
  1464.  
  1465. BEGIN
  1466.     window := Windows.FrontWindow();    (*we only adjust the cursor when we are in front*)
  1467.     IF (¬gInBackground) AND (¬IsDAWindow(window)) THEN
  1468.         (*calculate regions for different cursor shapes*)
  1469.         arrowRgn := Quickdraw.NewRgn();
  1470.         iBeamRgn := Quickdraw.NewRgn();
  1471.  
  1472.         (*start with a big, big rectangular region*)
  1473.         Quickdraw.SetRectRgn(arrowRgn, kExtremeNeg, kExtremeNeg, kExtremePos, kExtremePos);
  1474.  
  1475.         (*calculate iBeamRgn*)
  1476.         IF IsAppWindow(window) THEN
  1477.             (*$TYPECHECK- type checks off*)
  1478.             iBeamRect := window(DocumentPeek).docTE.viewRect;
  1479.             (*$TYPECHECK+ type checks on*)
  1480.             Quickdraw.SetPort(window);                    (*make a global version of the viewRect*)
  1481.             Quickdraw.LocalToGlobal(iBeamRect.topLeft);
  1482.             Quickdraw.LocalToGlobal(iBeamRect.botRight);
  1483.             Quickdraw.RectRgn(iBeamRgn, iBeamRect);
  1484.             Quickdraw.SetOrigin(-window.portBits.bounds.topLeft.h,
  1485.                                 -window.portBits.bounds.topLeft.v);
  1486.             Quickdraw.SectRgn(iBeamRgn, window^.visRgn, iBeamRgn);
  1487.             Quickdraw.SetOrigin(0, 0);
  1488.         END;
  1489.  
  1490.         (*subtract other regions from arrowRgn*)
  1491.         Quickdraw.DiffRgn(arrowRgn, iBeamRgn, arrowRgn);
  1492.         
  1493.         (*change the cursor and the region parameter*)
  1494.         IF Quickdraw.PtInRgn(mouse, iBeamRgn) THEN
  1495.             aCursor:=ToolUtils.GetCursor(ToolUtils.iBeamCursor);
  1496.             Quickdraw.SetCursor(aCursor^);
  1497.             Quickdraw.CopyRgn(iBeamRgn, region);
  1498.         ELSE
  1499.             Quickdraw.SetCursor(Quickdraw.arrow);
  1500.             Quickdraw.CopyRgn(arrowRgn, region);
  1501.         END;
  1502.  
  1503.         (*get rid of our local regions*)
  1504.         Quickdraw.DisposeRgn(arrowRgn);
  1505.         Quickdraw.DisposeRgn(iBeamRgn);
  1506.     END;
  1507. END AdjustCursor;
  1508.  
  1509.  
  1510. (*$S Main*)
  1511. PROCEDURE DoEvent(event: Events.EventRecord);
  1512.  
  1513. (*Do the right thing for an event. Determine what kind of event it is, and call
  1514.  the appropriate routines.*)
  1515.  
  1516. VAR
  1517.     part, err    : INTEGER;
  1518.     window        : Windows.WindowPtr;
  1519.     key            : CHAR;
  1520.     ignore        : BOOLEAN;
  1521.     aPoint        : Types.Point;
  1522.  
  1523. BEGIN
  1524.     CASE event.what OF
  1525.         Events.nullEvent:
  1526.             DoIdle|
  1527.         Events.mouseDown:
  1528.             part := Windows.FindWindow(event.where, window);
  1529.             CASE part OF
  1530.                 Windows.inMenuBar:
  1531.                     AdjustMenus;
  1532.                     DoMenuCommand(Menus.MenuSelect(event.where))|
  1533.                 Windows.inSysWindow:
  1534.                     Desk.SystemClick(event, window)|
  1535.                 Windows.inContent:
  1536.                     IF window # Windows.FrontWindow() THEN
  1537.                         Windows.SelectWindow(window);
  1538.                         (*DoEvent(event);*)    (*use this line for "do first click"*)
  1539.                     ELSE
  1540.                         DoContentClick(window, event)
  1541.                     END|
  1542.                 Windows.inDrag:
  1543.                     Windows.DragWindow(window, event.where, Quickdraw.screenBits.bounds)|
  1544.                 Windows.inGrow:
  1545.                     DoGrowWindow(window, event)|
  1546.                 Windows.inGoAway:
  1547.                     IF Windows.TrackGoAway(window, event.where) THEN
  1548.                         ignore := DoCloseWindow(window);        (*we don't care if cancelled*)
  1549.                     END|
  1550.                 Windows.inZoomIn, Windows.inZoomOut:
  1551.                     IF Windows.TrackBox(window, event.where, part) THEN
  1552.                         DoZoomWindow(window, part)
  1553.                     END|
  1554.                 ELSE (* ignore other parts *)
  1555.             END|
  1556.         Events.keyDown, Events.autoKey:
  1557.             key := CHR(BAND(event.message, Events.charCodeMask));
  1558.             IF BAND(event.modifiers, Events.cmdKey) # 0 THEN    (*Command key down*)
  1559.                 IF event.what = Events.keyDown THEN
  1560.                     AdjustMenus;            (*enable/disable/check menu items properly*)
  1561.                     DoMenuCommand(Menus.MenuKey(key));
  1562.                 END;
  1563.             ELSE
  1564.                 DoKeyDown(event)
  1565.             END|                            (*call DoActivate with the window and...*)
  1566.         Events.activateEvt:                        (*TRUE for activate, FALSE for deactivate*)
  1567.             DoActivate(Windows.WindowPtr(event.message),
  1568.                        BAND(event.modifiers, Events.activeFlag) # 0)|
  1569.         Events.updateEvt:                            (*call DoUpdate with the window to update*)
  1570.             DoUpdate(Windows.WindowPtr(event.message))|
  1571.         (*1.01 - It is not a bad idea to at least call DIBadMount in response
  1572.          to a diskEvt, so that the user can format a floppy.*)
  1573.         Events.diskEvt:
  1574.             IF HiWrd(event.message) # Types.noErr THEN
  1575.                 Quickdraw.SetPt(aPoint, kDILeft, kDITop);
  1576.                 err := DiskInit.DIBadMount(aPoint, event.message);
  1577.             END|
  1578.         kOSEvent:
  1579.             CASE BAND(SYSTEM.ROT(event.message, 8), $FF) OF    (*high byte of message*)
  1580.                 kMouseMovedMessage:
  1581.                     DoIdle|                    (*mouse moved is also an idle event*)
  1582.                 kSuspendResumeMessage:
  1583.                     gInBackground := BAND(event.message, kResumeMask) = 0;
  1584.                     DoActivate(Windows.FrontWindow(), ¬gInBackground)|
  1585.                 ELSE (* ignore other events *)
  1586.             END|
  1587.         ELSE (* ignore other events *)
  1588.     END
  1589. END DoEvent;
  1590.  
  1591.  
  1592. (*$S Main*)
  1593. PROCEDURE EventLoop;
  1594.  
  1595. (*Get events forever, and handle them by calling DoEvent.
  1596.  Also call AdjustCursor each time through the loop.*)
  1597.  
  1598. VAR
  1599.     cursorRgn    : Quickdraw.RgnHandle;
  1600.     gotEvent    : BOOLEAN;
  1601.     event        : Events.EventRecord;
  1602.     mouse        : Types.Point;
  1603.  
  1604. BEGIN
  1605.     cursorRgn := Quickdraw.NewRgn();        (*we'll pass an empty region to WNE the first time thru*)
  1606.     LOOP
  1607.         IF gHasWaitNextEvent THEN    (*put us 'asleep' forever under MultiFinder*)
  1608.             GetGlobalMouse(mouse);        (*since we might go to sleep*)
  1609.             AdjustCursor(mouse, cursorRgn);
  1610.             gotEvent := Events.WaitNextEvent(Events.everyEvent, event, GetSleep(), cursorRgn);
  1611.         ELSE
  1612.             Desk.SystemTask;                    (*must be called if using GetNextEvent*)
  1613.             gotEvent := Events.GetNextEvent(Events.everyEvent, event);
  1614.         END;
  1615.         IF gotEvent THEN
  1616.             AdjustCursor(event.where, cursorRgn);    (*make sure we have the right cursor*)
  1617.             DoEvent(event);                (*Handle the event only if for us.*)
  1618.         ELSE
  1619.             DoIdle
  1620.         END;
  1621.         (*If you are using modeless dialogs that have editText items,
  1622.          you will want to call IsDialogEvent to give the caret a chance
  1623.          to blink, even if WNE/GNE returned FALSE. However, check FrontWindow
  1624.          for a non-NIL value before calling IsDialogEvent.*)
  1625.     END;    (*loop forever*)
  1626. END EventLoop;
  1627.  
  1628.  
  1629. PROCEDURE _DataInit; EXTERNAL;
  1630.  
  1631. (*This routine is automatically linked in by the MPW Linker. This external
  1632.  reference to it is done so that we can unload its segment, %A5Init.*)
  1633.  
  1634.  
  1635. (*$S Main*)
  1636. (*$MAIN*)
  1637. BEGIN
  1638.     SegLoad.UnloadSeg(SYSTEM.ADR(_DataInit));    (*note that _DataInit must not be in Main!*)
  1639.     
  1640.     (*1.01 - call to ForceEnvirons removed*)
  1641.     (*If you have stack requirements that differ from the default,
  1642.      then you could use SetApplLimit to increase StackSpace at 
  1643.      this point, before calling MaxApplZone.*)
  1644.      
  1645.     Memory.MaxApplZone;            (*expand the heap so code segments load at the top*)
  1646.  
  1647.     Initialize;                (*initialize the program*)
  1648.     SegLoad.UnloadSeg(SYSTEM.ADR(Initialize));    (*note that Initialize must not be in Main!*)
  1649.  
  1650.     EventLoop;                (*call the main event loop*)
  1651. END TESample.
  1652.